package com.huawei.hms.utils;

import android.app.Activity;
import android.app.ActivityManager;
import android.app.ActivityManager.RunningAppProcessInfo;
import android.app.KeyguardManager;
import android.content.Context;
import android.content.ServiceConnection;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.content.res.Resources.NotFoundException;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.text.TextUtils;
import com.huawei.hms.android.HwBuildEx.VERSION;
import com.huawei.hms.framework.network.restclient.hwhttp.dns.DnsConfig;
import com.huawei.hms.support.common.ActivityMgr;
import com.huawei.hms.support.log.HMSLog;
import com.huawei.hwid.core.constants.HwAccountConstants;
import com.tencent.mm.opensdk.modelmsg.WXMediaMessage;
import java.lang.reflect.InvocationTargetException;
import java.util.List;
import java.util.Locale;
import java.util.regex.Pattern;
import o.avi;

public class Util {
    public static boolean isChinaROM() {
        Object a = a();
        if (!TextUtils.isEmpty(a)) {
            return HwAccountConstants.DEFAULT_SIMPLE_COUNTRY_CODE.equalsIgnoreCase(a);
        }
        a = b();
        if (!TextUtils.isEmpty(a)) {
            return a.toLowerCase(Locale.US).contains(HwAccountConstants.DEFAULT_SIMPLE_COUNTRY_CODE);
        }
        a = c();
        if (TextUtils.isEmpty(a)) {
            return false;
        }
        return HwAccountConstants.DEFAULT_SIMPLE_COUNTRY_CODE.equalsIgnoreCase(a);
    }

    private static String a() {
        String str = "";
        return getSystemProperties("ro.product.locale.region", "");
    }

    public static String getSystemProperties(String str, String str2) {
        try {
            Class cls = Class.forName("android.os.SystemProperties");
            return (String) cls.getDeclaredMethod("get", new Class[]{String.class, String.class}).invoke(cls, new Object[]{str, str2});
        } catch (ClassNotFoundException e) {
        } catch (NoSuchMethodException e2) {
        } catch (IllegalAccessException e3) {
        } catch (IllegalArgumentException e4) {
        } catch (InvocationTargetException e5) {
        } catch (ClassCastException e6) {
        }
        HMSLog.e("Util", "An exception occurred while reading: getSystemProperties:" + str);
        return str2;
    }

    private static String b() {
        String str = "";
        return getSystemProperties("ro.product.locale", "");
    }

    private static String c() {
        String str = "";
        Locale locale = Locale.getDefault();
        if (locale != null) {
            return locale.getCountry();
        }
        return str;
    }

    public static String getAppId(Context context) {
        Object ˊ = avi.ॱ(context).ˊ("client/app_id");
        return !TextUtils.isEmpty(ˊ) ? ˊ : a(context);
    }

    private static String a(Context context) {
        PackageManager packageManager = context.getPackageManager();
        if (packageManager == null) {
            HMSLog.e("Util", "In getMetaDataAppId, Failed to get 'PackageManager' instance.");
            return "";
        }
        try {
            ApplicationInfo applicationInfo = packageManager.getApplicationInfo(context.getPackageName(), DnsConfig.MAX_CACHE_ENTRIES);
            if (!(applicationInfo == null || applicationInfo.metaData == null)) {
                Object obj = applicationInfo.metaData.get("com.huawei.hms.client.appid");
                if (obj != null) {
                    String valueOf = String.valueOf(obj);
                    if (valueOf.startsWith("appid=")) {
                        return valueOf.substring("appid=".length());
                    }
                    return valueOf;
                }
            }
            HMSLog.e("Util", "In getMetaDataAppId, Failed to read meta data for the AppID.");
            return "";
        } catch (NameNotFoundException e) {
            HMSLog.e("Util", "In getMetaDataAppId, Failed to read meta data for the AppID.");
            return "";
        }
    }

    public static int getHmsVersion(Context context) {
        PackageManager packageManager = context.getPackageManager();
        if (packageManager == null) {
            HMSLog.e("Util", "In getHmsVersion, Failed to get 'PackageManager' instance.");
            return 0;
        }
        try {
            ApplicationInfo applicationInfo = packageManager.getApplicationInfo(context.getPackageName(), DnsConfig.MAX_CACHE_ENTRIES);
            if (!(applicationInfo == null || applicationInfo.metaData == null)) {
                Object obj = applicationInfo.metaData.get("com.huawei.hms.version");
                if (obj != null) {
                    String valueOf = String.valueOf(obj);
                    if (!TextUtils.isEmpty(valueOf)) {
                        return b(valueOf);
                    }
                }
            }
            HMSLog.i("Util", "In getHmsVersion, Failed to read meta data for the HMS VERSION.");
            return 0;
        } catch (NameNotFoundException e) {
            HMSLog.e("Util", "In getHmsVersion, Failed to read meta data for the HMS VERSION.");
            return 0;
        }
    }

    private static boolean a(String str) {
        if (Pattern.compile("(^([0-9]{1,2}\\.){2}[0-9]{1,2}$)|(^([0-9]{1,2}\\.){3}[0-9]{1,3}$)").matcher(str).find()) {
            return true;
        }
        return false;
    }

    private static int b(String str) {
        if (!a(str)) {
            return 0;
        }
        String[] split = str.split("\\.");
        if (split.length < 3) {
            return 0;
        }
        int parseInt = ((Integer.parseInt(split[0]) * 10000000) + (Integer.parseInt(split[1]) * 100000)) + (Integer.parseInt(split[2]) * 1000);
        if (split.length == 4) {
            return parseInt + Integer.parseInt(split[3]);
        }
        return parseInt;
    }

    public static String getCpId(Context context) {
        Object ˊ = avi.ॱ(context).ˊ("client/cp_id");
        return !TextUtils.isEmpty(ˊ) ? ˊ : b(context);
    }

    private static String b(Context context) {
        PackageManager packageManager = context.getPackageManager();
        if (packageManager == null) {
            HMSLog.e("Util", "In getMetaDataCpId, Failed to get 'PackageManager' instance.");
            return "";
        }
        try {
            ApplicationInfo applicationInfo = packageManager.getApplicationInfo(context.getPackageName(), DnsConfig.MAX_CACHE_ENTRIES);
            if (!(applicationInfo == null || applicationInfo.metaData == null)) {
                String string = applicationInfo.metaData.getString("com.huawei.hms.client.cpid", "");
                if (string.startsWith("cpid=")) {
                    return string.substring("cpid=".length());
                }
            }
            HMSLog.i("Util", "In getMetaDataCpId, Failed to read meta data for the CpId.");
            return "";
        } catch (NameNotFoundException e) {
            HMSLog.e("Util", "In getMetaDataCpId, Failed to read meta data for the CpId.");
            return "";
        }
    }

    public static boolean getBiSetting(Context context) {
        if (context == null) {
            HMSLog.e("Util", "In getBiSetting, context is null.");
            return false;
        }
        PackageManager packageManager = context.getPackageManager();
        if (packageManager == null) {
            HMSLog.e("Util", "In getBiSetting, Failed to get 'PackageManager' instance.");
            return false;
        }
        try {
            ApplicationInfo applicationInfo = packageManager.getApplicationInfo(context.getPackageName(), DnsConfig.MAX_CACHE_ENTRIES);
            if (applicationInfo != null && applicationInfo.metaData != null) {
                return applicationInfo.metaData.getBoolean("com.huawei.hms.client.bi.setting");
            }
            HMSLog.i("Util", "In getBiSetting, Failed to read meta data bisetting.");
            return false;
        } catch (NameNotFoundException e) {
            HMSLog.e("Util", "In getBiSetting, Failed to read meta data bisetting.");
            return false;
        }
    }

    public static String getAppName(Context context, String str) {
        if (context == null) {
            HMSLog.e("Util", "In getAppName, context is null.");
            return "";
        }
        PackageManager packageManager = context.getPackageManager();
        if (packageManager == null) {
            HMSLog.e("Util", "In getAppName, Failed to get 'PackageManager' instance.");
            return "";
        }
        try {
            if (TextUtils.isEmpty(str)) {
                str = context.getPackageName();
            }
            CharSequence applicationLabel = packageManager.getApplicationLabel(packageManager.getApplicationInfo(str, 0));
            return applicationLabel == null ? "" : applicationLabel.toString();
        } catch (NameNotFoundException e) {
            HMSLog.e("Util", "In getAppName, Failed to get app name.");
            return "";
        } catch (NotFoundException e2) {
            HMSLog.e("Util", "In getAppName, Failed to get app name.");
            return "";
        }
    }

    public static void unBindServiceCatchException(Context context, ServiceConnection serviceConnection) {
        try {
            context.unbindService(serviceConnection);
        } catch (Exception e) {
            HMSLog.e("Util", "On unBindServiceException:" + e.getMessage());
        }
    }

    public static int compareHmsVersion(String str, String str2) {
        if (TextUtils.isEmpty(str)) {
            return -1;
        }
        if (TextUtils.isEmpty(str2)) {
            return 1;
        }
        if (a(str) && a(str2)) {
            String[] split = str.split("\\.");
            String[] split2 = str2.split("\\.");
            if (Integer.parseInt(split[0]) > Integer.parseInt(split2[0])) {
                return 1;
            }
            if (Integer.parseInt(split[0]) < Integer.parseInt(split2[0])) {
                return -1;
            }
            if (Integer.parseInt(split[1]) > Integer.parseInt(split2[1])) {
                return 1;
            }
            if (Integer.parseInt(split[1]) < Integer.parseInt(split2[1])) {
                return -1;
            }
            if (Integer.parseInt(split[2]) > Integer.parseInt(split2[2])) {
                return 1;
            }
            if (Integer.parseInt(split[2]) < Integer.parseInt(split2[2])) {
                return -1;
            }
        }
        return 0;
    }

    public static Activity getValidActivity(Activity activity, Activity activity2) {
        if (activity != null && !activity.isFinishing()) {
            return activity;
        }
        if (activity2 == null || activity2.isFinishing()) {
            return null;
        }
        return activity2;
    }

    public static boolean isActivityFullscreen(Activity activity) {
        if ((activity.getWindow().getAttributes().flags & WXMediaMessage.DESCRIPTION_LENGTH_LIMIT) == WXMediaMessage.DESCRIPTION_LENGTH_LIMIT) {
            return true;
        }
        return false;
    }

    public static boolean isEMUI() {
        HMSLog.i("Util", "is Emui :" + VERSION.EMUI_SDK_INT);
        if (VERSION.EMUI_SDK_INT <= 0) {
            return false;
        }
        return true;
    }

    public static boolean isBackground(Context context) {
        boolean z = false;
        if (context == null) {
            return true;
        }
        ActivityManager activityManager = (ActivityManager) context.getSystemService("activity");
        KeyguardManager keyguardManager = (KeyguardManager) context.getSystemService("keyguard");
        if (activityManager == null || keyguardManager == null) {
            return true;
        }
        List<RunningAppProcessInfo> runningAppProcesses = activityManager.getRunningAppProcesses();
        if (runningAppProcesses == null) {
            return true;
        }
        for (RunningAppProcessInfo runningAppProcessInfo : runningAppProcesses) {
            if (TextUtils.equals(runningAppProcessInfo.processName, context.getPackageName())) {
                boolean z2;
                HMSLog.i("Util", "appProcess.importance is " + runningAppProcessInfo.importance);
                if (runningAppProcessInfo.importance == 100) {
                    z2 = true;
                } else {
                    z2 = false;
                }
                boolean inKeyguardRestrictedInputMode = keyguardManager.inKeyguardRestrictedInputMode();
                HMSLog.i("Util", "isForground is " + z2 + "***  isLockedState is " + inKeyguardRestrictedInputMode);
                if (!z2 || inKeyguardRestrictedInputMode) {
                    z = true;
                }
                return z;
            }
        }
        return true;
    }

    public static String getNetType(Context context) {
        String str = "";
        if (context != null) {
            ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService("connectivity");
            if (connectivityManager != null) {
                NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
                if (activeNetworkInfo != null && activeNetworkInfo.isAvailable()) {
                    return activeNetworkInfo.getTypeName();
                }
            }
        }
        return str;
    }

    public static Activity getActiveActivity(Activity activity, Context context) {
        if (isBackground(context)) {
            return null;
        }
        if (activity == null || activity.isFinishing()) {
            return ActivityMgr.INST.getCurrentActivity();
        }
        return activity;
    }
}
